package com.onlinemarket.thrid.daoimpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.onlinemarket.thrid.dao.goodsDao;
import com.onlinemarket.thrid.domain.Goods;

@Repository
public class goodsDaoImpl implements goodsDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean insertGood(Goods goods) {
		// TODO Auto-generated method stub
		String sql = "insert into goods (goods_name,goods_price,goods_number,supplier_id,warehouse_id) values(?,?,?,?,?)";
		int insertnumber = jdbcTemplate.update(sql, new Object[] { goods.getGoods_name(), goods.getGoods_price(),
				goods.getGoods_number(), goods.getSupplier_id(), goods.getWarehouse_id() });
		return insertnumber > 0 ? true : false;
	}

	@Override
	public Goods selectGoodsByName(String goodsname) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deletegoodsbyName(String goodsname) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Goods> getallGoods() {
		List<Goods> allgood = new ArrayList<Goods>();
		Goods goods = null;

		String sql = "select * from goods";
		List<java.util.Map<String, Object>> allgoods = jdbcTemplate.queryForList(sql);
		Iterator<java.util.Map<String, Object>> iterator = allgoods.iterator();
		while (iterator.hasNext()) {
			java.util.Map<String, Object> map = iterator.next();
			goods = new Goods(Integer.parseInt(map.get("goods_id").toString()), map.get("goods_name").toString(),
					Double.parseDouble(map.get("goods_price").toString()),
					Integer.parseInt(map.get("goods_number").toString()),
					Integer.parseInt(map.get("supplier_id").toString()),
					Integer.parseInt(map.get("warehouse_id").toString()));
			allgood.add(goods);
		}

		return allgood;
	}

	@Override
	public Goods getGoodsById(Integer goods_id) {
		Goods goods = new Goods();
		String sql = "select goods_id,goods_name,goods_price,goods_number,supplier_name,warehouse_name "
				+ "from goods,supplier,warehouse " + "where warehouse.warehouse_id=goods.warehouse_id "
				+ "and goods.supplier_id= supplier.supplier_id " + "AND goods_id=?";
		List<java.util.Map<String, Object>> allgoods = jdbcTemplate.queryForList(sql, new Object[] { goods_id });
		Iterator<java.util.Map<String, Object>> iterator = allgoods.iterator();
		while (iterator.hasNext()) {
			java.util.Map<String, Object> map = iterator.next();
			goods.setGoods_id(Integer.parseInt(map.get("goods_id").toString()));
			goods.setGoods_name(map.get("goods_name").toString());
			goods.setGoods_price(Double.parseDouble(map.get("goods_price").toString()));
			goods.setGoods_number(Integer.parseInt(map.get("goods_number").toString()));
			goods.setSupplier_name(map.get("supplier_name").toString());
			goods.setWarehouse_name(map.get("warehouse_name").toString());
		}
		return goods;
	}

	@Override
	public boolean updateGoodsinfo(Goods goods) {
		String sql = "update goods set goods_name=?,goods_price=?,goods_number=?,supplier_id=?,warehouse_id=? where goods_id=?";
		int updatnumber = jdbcTemplate.update(sql, new Object[] { goods.getGoods_name(), goods.getGoods_price(),
				goods.getGoods_number(), goods.getSupplier_id(), goods.getWarehouse_id(), goods.getGoods_id() });
		return updatnumber == 1 ? true : false;
	}

	@Override
	public List<Goods> getGoodsByIdforlist(Integer goods_id) {
		List<Goods> allgood = new ArrayList<Goods>();
		Goods goods = null;
		String sql = "select * from goods where goods_id=?";
		List<java.util.Map<String, Object>> allgoods = jdbcTemplate.queryForList(sql, new Object[] { goods_id });
		Iterator<java.util.Map<String, Object>> iterator = allgoods.iterator();
		while (iterator.hasNext()) {
			java.util.Map<String, Object> map = iterator.next();
			goods = new Goods(Integer.parseInt(map.get("goods_id").toString()), map.get("goods_name").toString(),
					Double.parseDouble(map.get("goods_price").toString()),
					Integer.parseInt(map.get("goods_number").toString()),
					Integer.parseInt(map.get("supplier_id").toString()),
					Integer.parseInt(map.get("warehouse_id").toString()));
			allgood.add(goods);
		}
		return allgood;
	}

}
